<?php
App::uses('AppController', 'Controller');
/**
 * Products Controller
 *
 * @property Product $Product
 * @property PaginatorComponent $Paginator
 */
class ProductsController extends AppController {
    
   
/**
 * Components
 *
 * @var array
 */

 public $components = array(
                'Session',
                'Paginator',
                'Auth' => array(
                    'authorize' => array('Controller'),
                    'loginAction' => array('controller' => 'users', 'action' => 'login'),
                    'loginRedirect' => array('controller' => 'students', 'action' => 'index'),
                    'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home')
                ),
                'Cookie' => array('name' => 'CookieMonster')
    );
  public function isAuthorized($user) {
                return true;
        }
  public function beforeFilter() {
      
//        $this->Auth->allow('index');
    }
   

 /* index method
 *
 * @return void
 */
	public function index() {
		$this->Paginator->settings = array(
        'limit' => 2
		  );
		$this->Product->recursive = 0;
		$this->set('products', $this->Paginator->paginate());
  
	}
       

        /**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Product->exists($id)) {
			throw new NotFoundException(__('Invalid product'));
		}
		$options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
		$this->set('product', $this->Product->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
            
		if ($this->request->is('post')) {
                   // print_r($this->request->data);
			$this->Product->create();
                        $validate = true;
                            $allowedMime = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/jpg');

                            // Control Picture of Before
                            $requestimage = $this->request->data['Product']['picture1'];

                            if (!empty($requestimage['type']) && in_array($requestimage['type'], $allowedMime)) {
                                $newfilename = date("YmdGis") . $requestimage['name'];
                                if (!move_uploaded_file($requestimage['tmp_name'], WWW_ROOT . 'img' . DS . 'uploads' . DS . $newfilename)) {
                                    $this->Product->invalidate('picture1', 'File was not uploaded, please try again.');
                                    $validate = false;
                                } else {
                                    $this->request->data['Product']['picture1'] = $newfilename;
                                }
                            } else {
                                if (!empty($requestimage['tmp_name'])) {
                                    $this->Product->invalidate('picture1', 'Please supply a valid image (.jpg, .gif or .png)');
                                    $validate = false;
                                } else {
                                    $this->request->data['Product']['picture1'] = NULL;
                                }
                            }
                            // Control Picture2 of Before
                            $requestimage2 = $this->request->data['Product']['picture2'];

                            if (!empty($requestimage2['type']) && in_array($requestimage2['type'], $allowedMime)) {
                                $newfilename2 = date("YmdGis") . $requestimage2['name'];
                                if (!move_uploaded_file($requestimage2['tmp_name'], WWW_ROOT . 'img' . DS . 'uploads' . DS . $newfilename2)) {
                                    $this->Product->invalidate('picture2', 'File was not uploaded, please try again.');
                                    $validate = false;
                                } else {
                                    $this->request->data['Product']['picture2'] = $newfilename2;
                                }
                            } else {
                                if (!empty($requestimage2['tmp_name'])) {
                                    $this->Product->invalidate('picture2', 'Please supply a valid image (.jpg, .gif or .png)');
                                    $validate = false;
                                } else {
                                    $this->request->data['Product']['picture2'] = NULL;
                                }
                            }
                            // Control Picture3 of Before
                            $requestimage3 = $this->request->data['Product']['picture2'];

                            if (!empty($requestimage3['type']) && in_array($requestimage3['type'], $allowedMime)) {
                                $newfilename3 = date("YmdGis") . $requestimage3['name'];
                                if (!move_uploaded_file($requestimage3['tmp_name'], WWW_ROOT . 'img' . DS . 'uploads' . DS . $newfilename3)) {
                                    $this->Product->invalidate('picture3', 'File was not uploaded, please try again.');
                                    $validate = false;
                                } else {
                                    $this->request->data['Product']['picture3'] = $newfilename3;
                                }
                            } else {
                                if (!empty($requestimage3['tmp_name'])) {
                                    $this->Product->invalidate('picture3', 'Please supply a valid image (.jpg, .gif or .png)');
                                    $validate = false;
                                } else {
                                    $this->request->data['Product']['picture3'] = NULL;
                                }
                            }
                             // Control Picture4 of Before
                            $requestimage3 = $this->request->data['Product']['picture4'];

                            if (!empty($requestimage4['type']) && in_array($requestimage4['type'], $allowedMime)) {
                                $newfilename4 = date("YmdGis") . $requestimage4['name'];
                                if (!move_uploaded_file($requestimage3['tmp_name'], WWW_ROOT . 'img' . DS . 'uploads' . DS . $newfilename4)) {
                                    $this->Product->invalidate('picture4', 'File was not uploaded, please try again.');
                                    $validate = false;
                                } else {
                                    $this->request->data['Product']['picture4'] = $newfilename4;
                                }
                            } else {
                                if (!empty($requestimage4['tmp_name'])) {
                                    $this->Product->invalidate('picture4', 'Please supply a valid image (.jpg, .gif or .png)');
                                    $validate = false;
                                } else {
                                    $this->request->data['Product']['picture4'] = NULL;
                                }
                            }
			 if ($validate && $this->Product->saveAssociated($this->request->data)) {
				$this->Session->setFlash(__('The product has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The product could not be saved. Please, try again.'));
			}
		}
                Controller::loadModel('Category');
                $nodelist = $this->Category->generateTreeList(null,null,null," - ");
               
//			$this->set('parents',$this->Category->generatetreelist());
                $this->set('parents',($nodelist));
	//	$categories = $this->Product->Category->find('list', array('conditions'=>array('Category.lft >'=>0)));
		$users = $this->Product->User->find('list');
		$locations = $this->Product->Location->find('list');
		$this->set(compact('users', 'locations'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Product->exists($id)) {
			throw new NotFoundException(__('Invalid product'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Product->save($this->request->data)) {
				$this->Session->setFlash(__('The product has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The product could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
			$this->request->data = $this->Product->find('first', $options);
		}
		$categories = $this->Product->Category->find('list');
		$users = $this->Product->User->find('list');
		$locations = $this->Product->Location->find('list');
		$this->set(compact('categories', 'users', 'locations'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Product->id = $id;
		if (!$this->Product->exists()) {
			throw new NotFoundException(__('Invalid menu'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Product->delete()) {
			$this->Session->setFlash(__('The menu has been deleted.'));
		} else {
			$this->Session->setFlash(__('The menu could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}}
